<?php



class Pegawai extends Controller {



    function Pegawai() {

        parent::Controller();

        $this->load->model('Pegawai_model', 'p');

    }

    

    function index() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }

        

        $data['additional_js'] = '

        <script type="text/javascript">

        function deleteRow(id){

        var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';

                url = "'.base_url().'index.php/pegawai/hapus/"+id;

             $.ajax({

               type: "POST",

               url: "'.base_url().'index.php/pegawai/pegawai_bisa_dihapus/"+id,

               success: function(msg) {

                 if (msg==\'bisa\') {

                    $.prompt(txt,{

                        buttons:{Hapus:true, Batal:false},

                        callback: function(v){

                            if(v) {

                                $("tr#row-"+id).fadeOut("slow", function() {

                                    $("tr#row-"+id).remove();

                                });

                                top.location.href=url;

                            }

                        }

                    });

                 } else {

                    alert(\'Maaf, data user tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');

                 }

               }

            });    

            } </script>    

            ';

            
        
        $where = '';

        $data['cari'] = $this->input->post('cari')?$this->input->post('cari'):'' ;
        
            
        if( $data['role'] == 0)

        { 

                 

          $this->db->like('CONCAT(nama,nip,nip_baru)', $this->session->userdata('nip'));

          $where = "WHERE CONCAT(nama,nip,nip_baru) LIKE '%".$this->session->userdata('nip')."%'" ;

          

        }

            

        elseif( $_SERVER['REQUEST_METHOD']=='POST' || $this->uri->segment(2)=='index' )

        { 

          if($this->input->post('cari') && $this->input->post('instansi')){
            
            if($this->session->userdata('id_akses') != "" || $this->session->userdata('akses') > 0)         
            $this->db->where('id_instansi',$this->session->userdata('id_akses'));
            else
            $this->db->where('id_instansi', $this->input->post('instansi'));

            $this->db->like('CONCAT(nama,nip,nip_baru)', $this->input->post('cari'));

            

            $where .= "WHERE CONCAT(nama,nip,nip_baru) LIKE '%".$this->input->post('cari')."%'" ;

            if($this->session->userdata('id_akses') != "" || $this->session->userdata('akses') > 0)
            $where .= " AND id_instansi = ".$this->session->userdata('id_akses');
            else
            $where .= " AND id_instansi = '".$this->input->post('instansi')."'" ; 

          }

          

          if($this->input->post('cari')) {

            $this->db->like('CONCAT(nama,nip,nip_baru)', $this->input->post('cari'));

            $where = "WHERE CONCAT(nama,nip,nip_baru) LIKE '%".$this->input->post('cari')."%'" ;
            if($this->session->userdata('id_akses') != "" || $this->session->userdata('akses') > 0)
            {  
            $this->db->where('id_instansi',$this->session->userdata('id_akses'));
            $where .= " AND id_instansi = ".$this->session->userdata('id_akses');
            }
         
          }

          if($this->input->post('instansi')) {
            if($this->session->userdata('id_akses') != "" || $this->session->userdata('akses') > 0)
            {  
            $this->db->where('id_instansi',$this->session->userdata('id_akses'));
            $where = " WHERE id_instansi = ".$this->session->userdata('id_akses');
            }
            else {     
            $this->db->where('id_instansi', $this->input->post('instansi'));

            $where = "WHERE id_instansi = '".$this->input->post('instansi')."'" ;
            }
          }

          

        } else {
           if($this->session->userdata('id_akses') != "" || $this->session->userdata('akses') > 0)
            {  
            $this->db->where('id_instansi',$this->session->userdata('id_akses'));
            $where .= " WHERE id_instansi = ".$this->session->userdata('id_akses');
            } 
            
        }

        

        

        $data['judul'] = 'LIST DATA PEGAWAI';

        $data['list_data'] = $this->p->list_pegawai_header().$this->p->list_pegawai_isi('','',$this->uri->segment(3),20);

        

        $this->load->library('pagination');

        

        $config['base_url'] = site_url().'/pegawai/index/';

        $config['total_rows'] = $this->db->count_all('pegawai '.$where);        

        $config['per_page'] = '20';

        $config['num_links'] = 10;

        $config['uri_segment'] = 3;

        $config['js_link'] = 'doSubmit';

        

        $config['cur_tag_open'] = '<span class="current">';

        $config['cur_tag_close'] = '</span>';



        $this->pagination->initialize($config); 

        $data['paginasi'] = $this->pagination->create_links();

        

        $data['tampilkan_menu'] = true;
        
        if($this->input->post('instansi'))              
            $akses = $this->input->post('instansi');    
        else
            $akses = $this->session->userdata('id_akses');
            
        $data['filter'] = combo_instansi('instansi',$akses);

        

        $this->load->library('parser');

        if($this->input->post('page')) echo $data['list_data'];

        else $this->parser->parse('view_pegawai', $data);

        #$this->load->view('view_pegawai',$data);

    }



    function _getKabKota() {

        $this->db->order_by('nama', 'asc');

        $q = $this->db->get('kabkota');

        $list_kabkota = '';                                                    

        foreach ($q->result() as $row) {

            $list_kabkota .= '"'.$row->nama.'",';

        }

        $list_kabkota = substr($list_kabkota, 0, -1);

        return $list_kabkota;

    }

    

    function _setRules() {

        return '

                    //set the rules for the field names

                    rules: {

                        nama: { required: true },

                        nip: { required: true },

                        tempat_lahir: { required: true },

                        y: { required: true },

                        id_agama: { required: true },

                        goldar: { required: true },

                        id_jenis_kepegawaian: { required: true },

                        alamat: { required: true },

                        id_instansi: { required: true },

                        id_unit_kerja: { required: true }

                    },

                    //set messages to appear inline

                    messages: {

                        nama: "Nama wajib diisi",

                        nip: "NIP wajib diisi",

                        tempat_lahir: "Tempat Lahir wajib diisi",

                        y: "Tahun Kelahiran wajib diisi",

                        id_agama: "Agama wajib diisi",

                        goldar: "Golongan Darah wajib diisi",

                        id_jenis_kepegawaian: "Jenis Kepegawaian wajib diisi",

                        alamat: "Alamat wajib diisi",

                        id_instansi: "Instansi wajib diisi",

                        id_unit_kerja: "Unit Kerja wajib diisi"

                    }

        ';

    }

    

    function _setJsForm($additional_string) {

        $ret = '

        <script type="text/javascript">

            $(function() {

                $("#nama").focus();

                $("#tempat_lahir").autocompleteArray(['.$additional_string.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});

                $("#btn_kembali").click( function() {

                    window.location = \''.base_url().'index.php/pegawai\';

                });

                // validate contact form on keyup and submit

                $("form").validate({

                    '.$this->_setRules().'

                });

            });

        </script>

        ';

        return $ret;

    }

    

    function form_isian() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }

        

        $form_action = base_url().'index.php/pegawai/simpan';

        $form_id = 'form_pegawai';

        

        $list_kabkota = $this->_getKabKota();

        $data['additional_js'] = $this->_setJsForm($list_kabkota);

           

        $data['judul'] = 'INPUT DATA PEGAWAI';

        $data['list_data'] = $this->p->entry_pegawai($form_id,$form_action);

        $this->load->view('view_fpegawai', $data);

    }

    

    function simpan($is_update=FALSE, $id='') {

        if (!($this->input->post('nama')) && !($this->input->post('nip'))) {

            redirect('pegawai/form_edit/'.$id);

        } else {

            $arr['nama'] = $this->input->post('nama');

        }



        if ($this->input->post('gelar_depan')) {

            $arr['gelar_depan'] = $this->input->post('gelar_depan');

        } else {

            $arr['gelar_depan'] = "";

        }

        if ($this->input->post('gelar_belakang')) {

            $arr['gelar_belakang'] = $this->input->post('gelar_belakang');

        } else {

            $arr['gelar_belakang'] = "";

        }

        if ($this->input->post('nip')) {

            $arr['nip'] = $this->input->post('nip');

        } else {

            $arr['nip'] = "";

        }

        if ($this->input->post('nip_baru')) {

            $arr['nip_baru'] = $this->input->post('nip_baru');

        } else {

            $arr['nip_baru'] = "";

        }

        if ($this->input->post('tempat_lahir')) {

            $arr['tempat_lahir'] = $this->input->post('tempat_lahir');

        } else {

            $arr['tempat_lahir'] = "";

        }

        if ($this->input->post('d')) {

            $a = array($this->input->post('y'), $this->input->post('m'), $this->input->post('d'));

            $arr['tgl_lahir'] = implode(TGL_SEPARATOR, $a);

        } else {

            $arr['tgl_lahir'] = date("Y-m-d");

        }

        if ($this->input->post('jk')) {

            $arr['jk'] = $this->input->post('jk');

        } else {

            $arr['jk'] = "";

        }

        if ($this->input->post('status_perkawinan')) {

            $arr['status_perkawinan'] = $this->input->post('status_perkawinan');

        } else {

            $arr['status_perkawinan'] = "";

        }

        if ($this->input->post('id_agama')) {

            $arr['id_agama'] = $this->input->post('id_agama');

        } else {

            $arr['id_agama'] = "";

        }

        if ($this->input->post('goldar')) {

            $arr['goldar'] = $this->input->post('goldar');

        } else {

            $arr['goldar'] = "";

        }

        if ($this->input->post('id_jenis_kepegawaian')) {

            $arr['id_jenis_kepegawaian'] = $this->input->post('id_jenis_kepegawaian');

        } else {

            $arr['id_jenis_kepegawaian'] = "";

        }

        if ($this->input->post('alamat')) {

            $arr['alamat'] = $this->input->post('alamat');

        } else {

            $arr['alamat'] = "";

        }

        if ($this->input->post('id_instansi')) {

            $arr['id_instansi'] = $this->input->post('id_instansi');

        } else {

            $arr['id_instansi'] = "";

        }

        if ($this->input->post('id_unit_kerja')) {

            $arr['id_unit_kerja'] = $this->input->post('id_unit_kerja');

        } else {

            $arr['id_unit_kerja'] = "";

        }

        if ($this->input->post('id_unit_kerja')) {

            $arr['id_unit_kerja'] = $this->input->post('id_unit_kerja');

        } else {

            $arr['id_unit_kerja'] = "";

        }

        if ($this->input->post('no_askes')) {

            $arr['no_askes'] = $this->input->post('no_askes');

        } else {

            $arr['no_askes'] = "";

        }

        if ($this->input->post('npwp')) {

            $arr['npwp'] = $this->input->post('npwp');

        } else {

            $arr['npwp'] = "";

        }

        if ($this->input->post('status_bapertarum')) {

            $arr['status_bapertarum'] = $this->input->post('status_bapertarum');

        } else {

            $arr['status_bapertarum'] = "";

        }

        if ($this->input->post('no_karpeg')) {

            $arr['no_karpeg'] = $this->input->post('no_karpeg');

        } else {

            $arr['no_karpeg'] = "";

        }

        if ($this->input->post('no_karis_karsu')) {

            $arr['no_karis_karsu'] = $this->input->post('no_karis_karsu');

        } else {

            $arr['no_karis_karsu'] = "";

        }

        

        

        $config['upload_path'] = './foto_pegawai/';

        $config['allowed_types'] = 'gif|jpg|png';

        #$config['max_size'] = '100';

        #$config['max_width'] = '1024';

        #$config['max_height'] = '768';

        

        $this->load->library('upload', $config);

        

        if( $this->upload->do_upload('foto') )

        {

            $data = $this->upload->data();

            $arr['foto'] = $data['file_name'];

        }

        

        if ($is_update) {

             

            $this->p->simpan($arr, $is_update, $id);

        } else {           

            $this->p->simpan($arr);

        }



        redirect('pegawai', 'refresh');

    }

    

    function form_edit($id) {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }

        

        $form_action = base_url().'index.php/pegawai/simpan/true/'.$id;

        $form_id = 'form_pegawai';  

        $list_kabkota = $this->_getKabKota();

        $data['additional_js'] = $this->_setJsForm($list_kabkota);

        

        $this->db->where(array('idspg_pegawai' => $id));

        $q = $this->db->get('pegawai');

        if ($q->num_rows() > 0) {

            $row = $q->row();

            $arr['nip']                  = $row->nip;

            $arr['nip_baru']             = $row->nip_baru;

            $arr['nama']                 = $row->nama;

            $arr['alamat']               = $row->alamat;

            $arr['tempat_lahir']         = $row->tempat_lahir;

            $arr['id_agama']             = $row->id_agama;

            $arr['jk']                   = $row->jk;

            $arr['goldar']               = $row->goldar;

            $arr['id_unit_kerja']        = $row->id_unit_kerja;

            $arr['gelar_depan']          = $row->gelar_depan;

            $arr['gelar_belakang']       = $row->gelar_belakang;

            $arr['status_perkawinan']    = $row->status_perkawinan;

            $arr['id_instansi']          = $row->id_instansi;

            $arr['id_jenis_kepegawaian'] = $row->id_jenis_kepegawaian;

            $arr['tgl_lahir']            = trimTime($row->tgl_lahir);

            $arr['no_askes']             = $row->no_askes;

            $arr['npwp']                 = $row->npwp;

            $arr['status_bapertarum']    = $row->status_bapertarum;

            $arr['no_karpeg']            = $row->no_karpeg;

            $arr['no_karis_karsu']       = $row->no_karis_karsu;

            $arr['foto']                 = $row->foto;

            $arr['id']                   = $id;

            

            $form_action = base_url().'index.php/pegawai/simpan/true/'.$id;

            $form_id = 'form_pegawai';

            

            $data['judul'] = 'UBAH DATA PEGAWAI';

            $data['list_data'] = $this->p->entry_pegawai($form_id,$form_action,$arr);

            $this->load->view('view_fpegawai', $data);

        } else {

            redirect('pegawai', 'refresh');

        }

    }





    function hapus($id) {

        $this->db->where('idspg_pegawai', $id);

        $this->db->delete('pegawai');

        redirect('pegawai', 'refresh');

    }

    

    function hapus_foto($id) {

        $this->db->where('idspg_pegawai', $id);

        $this->db->update('pegawai',array('foto' => ''));

        redirect('pegawai/form_edit/'.$id);

    }

    

    

    function pegawai_bisa_dihapus($id) {

        echo 'bisa';

    }

    

}



/* End of file welcome.php */

/* Location: ./system/application/controllers/welcome.php */